

<template>
  <div class="register-container">
    <el-card class="register-form">
      <template #header>
        <h2>注册</h2>
      </template>
      <el-form ref="registerForm" :model="user" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="user.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="user.pid" placeholder="请输入身份证号"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
              type="password"
              v-model="user.password"
              placeholder="请输入密码"
              show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码">
          <el-input
              type="password"
              v-model="user.confirmPassword"
              placeholder="请确认密码"
              show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="user.email" placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item label="用户类型">
          <el-select v-model="user.userType" placeholder="请选择用户类型">
            <el-option label="普通用户" value="USER"></el-option>
            <el-option label="管理员" value="ADMIN"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              style="width: 100%"
              @click="register"
          >
            注册
          </el-button>
        </el-form-item>
      </el-form>
      <div class="login-link">
        已有账号？
        <router-link to="/login">登录</router-link>
      </div>
    </el-card>

    <!-- 自动开卡对话框 -->
    <el-dialog v-model="openCardDialogVisible" title="自动开卡" width="30%">
      <el-form :model="bankCardInfo" label-width="100px">
        <el-form-item label="是否自动开卡">
          <el-select v-model="autoOpenCard" placeholder="请选择">
            <el-option label="是" value="yes"></el-option>
            <el-option label="否" value="no"></el-option>
          </el-select>
        </el-form-item>
        <div v-if="autoOpenCard === 'yes'">
          <el-form-item label="币种 (curid)">
            <el-input v-model="bankCardInfo.curID" placeholder="请输入币种"></el-input>
          </el-form-item>
          <el-form-item label="银行卡密码">
            <el-input
                type="password"
                v-model="bankCardInfo.pass"
                placeholder="请输入银行卡密码"
                show-password
            ></el-input>
          </el-form-item>
        </div>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="openCardDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitOpenCard">
            确认
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { ElMessage } from "element-plus";

export default {
  data() {
    return {
      user: {
        username: "",
        password: "",
        confirmPassword: "",
        email: "",
        userType: "USER",
        auto_open_card: "no", // 默认为“否”
        curid: null,          // 默认为null
        pass: null,            // 默认为null
        pid: ""
      },
      bankCardInfo: {
        curID: "",
        pass: "",
      },
      autoOpenCard: "no",
      openCardDialogVisible: false,
    };
  },
  methods: {
    register() {
      if (this.user.password !== this.user.confirmPassword) {
        ElMessage.error("两次输入的密码不一致");
        return;
      }

      // 显示开卡对话框
      this.openCardDialogVisible = true;
    },

    submitOpenCard() {
      if (this.autoOpenCard === "yes") {
        if (!this.bankCardInfo.curID) {
          ElMessage.error("请输入币种");
          return;
        }
        if (!this.bankCardInfo.pass) {
          ElMessage.error("请输入银行卡密码");
          return;
        }

        // 将自助开卡信息添加到用户对象
        this.user.auto_open_card = this.autoOpenCard;
        this.user.curid = this.bankCardInfo.curID;
        this.user.pass = this.bankCardInfo.pass;

        // 注册用户
        this.registerUser();
      } else {
        // 直接注册用户
        this.registerUser();
      }

      this.openCardDialogVisible = false;
    },

    registerUser() {
      axios.post("http://localhost:8088/api/auth/register", this.user)
          .then((response) => {
            console.log("注册成功", response.data);
            ElMessage.success("注册成功");
            this.$router.push("/login");
          })
          .catch((error) => {
            console.error("注册失败", error);
            ElMessage.error("注册失败，请检查输入信息");
          });
    },
  },
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.register-form {
  width: 500px;
}

.login-link {
  margin-top: 20px;
  text-align: center;
}
</style>